﻿using System;
using Util;
using Util.Domains.Contexts;

namespace IterationLife.System.Models {
    /// <summary>
    /// 用户
    /// </summary>
    public partial class User {
        ///// <summary>
        ///// 登陆失败最大次数
        ///// </summary>
        //public static int MaxLoginFailTimes => 10;
        ///// <summary>
        ///// 安全令牌有效天数
        ///// </summary>
        //public static int TokenExpireDay => 7;

        ///// <summary>
        ///// 创建安全令牌
        ///// </summary>
        //public static string CreateToken() {
        //    return Encrypt.Md5By32( Guid.NewGuid().ToString() );
        //}

        ///// <summary>
        ///// 更新安全令牌
        ///// </summary>
        //public void UpdateToken() {
        //    if( !ValidateToken() )
        //        Token = CreateToken();
        //    TokenExpireDate = DateTime.Now.AddDays( TokenExpireDay );
        //}

        ///// <summary>
        ///// 验证安全令牌
        ///// </summary>
        //public bool ValidateToken() {
        //    return TokenExpireDate.SafeValue() > DateTime.Now;
        //}

        ///// <summary>
        ///// 删除
        ///// </summary>
        //public void Delete() {
        //    IsDeleted = true;
        //}

        ///// <summary>
        ///// 初始化
        ///// </summary>
        //public override void Init() {
        //    base.Init();
        //    if( !Password.IsEmpty() )
        //        return;
        //    InitPassword();
        //}

        ///// <summary>
        ///// 初始化密码
        ///// </summary>
        //public void InitPassword() {
        //    var password = Id.SafeString().ToLower().Replace( "-", "" ) + "1234567890";
        //    Password = Encrypt.Md5By32( password );
        //}


        /// <summary>
        /// 初始化用户
        /// </summary>
        public void InitUser() {
            //InitPassword();
            Enabled = true;
            IsLock = false;
            IsTest = false;
            CreationTime = DateTime.Now;
            CreatorId = ApplicationContext.User.UserId.ToGuidOrNull();
        }

        ///// <summary>
        ///// 更新用户
        ///// </summary>
        ///// <param name="userName">用户名</param>
        //public void UpdataUser( string userName ) {
        //    UserName = userName;
        //}

        ///// <summary>
        ///// 验证用户被冻结
        ///// </summary>
        //public void ValidateDisabled() {
        //    if( Enabled == false )
        //        throw new Warning( SystemResource.UserIsDisabled, ErrorCode.UserIsDisabled );
        //}

        ///// <summary>
        ///// 验证锁定
        ///// </summary>
        //public void ValidateLock() {
        //    if( IsLock == false )
        //        return;
        //    var remainTime = GetLockRemainTime();
        //    if( remainTime <= 0 )
        //        return;
        //    var message = GetLockMessage( remainTime );
        //    throw new Warning( message, ErrorCode.UserIsLocked );
        //}

        ///// <summary>
        ///// 获取剩余锁定时间
        ///// </summary>
        //private int GetLockRemainTime() {
        //    TimeSpan interval = Time.GetDateTime() - LockBeginTime.SafeValue();
        //    return GetLockTime() - Util.Helpers.Convert.ToInt( interval.TotalSeconds );
        //}

        ///// <summary>
        ///// 获取锁定时间
        ///// </summary>
        //private int GetLockTime() {
        //    if( LockDuration.SafeValue() == 0 )
        //        return 60;
        //    return LockDuration.SafeValue();
        //}

        ///// <summary>
        ///// 获取锁定消息
        ///// </summary>
        ///// <param name="remain">剩余锁定时间</param>
        //public string GetLockMessage( int remain ) {
        //    string message = LockMessage;
        //    if( message.IsEmpty() )
        //        message = SystemResource.LockOnLoginFail;
        //    return GetLockMessage( message, remain );
        //}

        ///// <summary>
        ///// 获取锁定消息
        ///// </summary>
        ///// <param name="message">锁定消息</param>
        ///// <param name="remain">剩余锁定时间</param>
        //public static string GetLockMessage( string message, int remain ) {
        //    return $"{message},剩余锁定时间:{remain}秒";
        //}

        ///// <summary>
        ///// 验证密码
        ///// </summary>
        ///// <param name="password">密码,加密前的明文</param>
        //public void ValidatePassword( string password ) {
        //    if( IsPasswordCorrect( password ) )
        //        return;
        //    throw new Warning( SystemResource.UserNameOrPasswordIsError, ErrorCode.PasswordIsError );
        //}

        ///// <summary>
        ///// 密码是否正确
        ///// </summary>
        ///// <param name="password">密码,加密前的明文</param>
        //public bool IsPasswordCorrect( string password ) {
        //    var pwd = GetEncryptPassword( Id, password );
        //    if( Password.SafeString().ToLower() == pwd.SafeString().ToLower() )
        //        return true;
        //    return false;
        //}

        ///// <summary>
        ///// 获取加密后的密码
        ///// </summary>
        ///// <param name="userId">用户编号</param>
        ///// <param name="password">密码,加密前的明文</param>
        //public static string GetEncryptPassword( Guid userId, string password ) {
        //    string text = userId.SafeString().ToLower().Replace( "-", "" ) + password;
        //    return Encrypt.Md5By32( text );
        //}

        ///// <summary>
        ///// 更新登录失败
        ///// </summary>
        //public void UpdateLoginFail() {
        //    LoginFailTimes = LoginFailTimes.SafeValue() + 1;
        //    if( LoginFailTimes >= MaxLoginFailTimes ) {
        //        Lock();
        //    }
        //}

        ///// <summary>
        ///// 锁定用户
        ///// </summary>
        //public void Lock() {
        //    IsLock = true;
        //    LockBeginTime = DateTime.Now;
        //}

        ///// <summary>
        ///// 更新登录成功
        ///// </summary>
        //public void UpdateLoginSuccess() {
        //    LastLoginIp = CurrentLoginIp;
        //    CurrentLoginIp = Net.Ip;
        //    LastLoginTime = CurrentLoginTime;
        //    CurrentLoginTime = DateTime.Now;
        //    LoginTimes = LoginTimes.SafeValue() + 1;
        //    IsLock = false;
        //    LockBeginTime = null;
        //    LoginFailTimes = null;
        //    LockMessage = string.Empty;
        //    LockDuration = null;
        //    UpdateToken();
        //}
    }
}